package com.scoy.honeymei.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.adapter.AddressAdapter;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.AddressBean;
import com.scoy.honeymei.bean.GoodsBean;
import com.scoy.honeymei.custem.LeftSwipeLayout;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.commonsdk.debug.I;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: AddressListActivity$
 * @Description: 地址列表
 * @Author: GaoZhan
 * @CreateDate: 2019/9/5/0005$ 下午 14:20$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/5/0005$ 下午 14:20$
 * @UpdateRemark: 更新内容
 */
public class AddressListActivity extends BaseActivity {


    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.normal_rv)
    RecyclerView normalRv;
    @BindView(R.id.nodata_tv)
    TextView nodataTv;
    @BindView(R.id.nor_srl)
    SmartRefreshLayout norSrl;
    @BindView(R.id.add_toadd_tv)
    TextView addToaddTv;
    private AddressListActivity mContext;
    private AddressAdapter addressAdapter;
    private ArrayList<AddressBean> dataList;
    private int type;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_list);
        ButterKnife.bind(this);
        mContext = AddressListActivity.this;
        type = getIntent().getIntExtra("wy", 0);
        initNormal();

        httpAddressList();

    }

    @Override
    public void initNormal() {
        dataList = new ArrayList<>();

        titleTv.setText(R.string.reciveaddress);

        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

        initRV();

        norSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(1500);
                dataList.clear();
                httpAddressList();

            }
        }).setEnableLoadMore(false);/*setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(1500);

            }
        });*/
    }

    private void initRV() {

        addressAdapter = new AddressAdapter(mContext, dataList, nodataTv);
        normalRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        DividerItemDecoration did = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        did.setDrawable(getResources().getDrawable(R.drawable.divider_gray_line_pd));
        normalRv.addItemDecoration(did);
        if (type != 1) {
            normalRv.addOnItemTouchListener(new LeftSwipeLayout.OnSwipeItemTouchListener(mContext));
        }
        normalRv.setAdapter(addressAdapter);
        addressAdapter.setOnOneClick(new AddressAdapter.OnOneClick() { //delete
            @Override
            public void oneClick(int pos) {
                AddressBean addressBean = dataList.get(pos);
                httpDetele(addressBean.getId());
            }
        });
        addressAdapter.setOnTwoClick(new AddressAdapter.OnTwoClick() { //edit
            @Override
            public void twoClick(int pos) {
                Intent intent = new Intent(mContext, AddAdressActivity.class);
                intent.putExtra("type", 1);
                intent.putExtra("addressBean", dataList.get(pos));
                startActivityForResult(intent, 0x112);
            }
        });

        addressAdapter.setOnThreeClick(new AddressAdapter.OnThreeClick() {
            @Override
            public void threeClick(int pos) {
                if (type == 1) {
                    Intent intent = new Intent();
                    intent.putExtra("address", dataList.get(pos));
                    setResult(0x124, intent);
                    finish();
                }
            }
        });

    }

    @OnClick({R.id.back_iv, R.id.add_toadd_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.add_toadd_tv:
                startActivityForResult(new Intent(mContext, AddAdressActivity.class), 0x124);
                break;
        }
    }

    //地址列表
    private void httpAddressList() {
        HttpParams params = new HttpParams();
        params.put("userid", SPHelper.getString(MeConstant.UID, ""));
        HpGo.newInstance().HttpGo(mContext, MyUrl.ADDRESS_LIST, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                List<AddressBean> data = new Gson().fromJson(o, new TypeToken<List<AddressBean>>() {
                }.getType());
                dataList.addAll(data);
                addressAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    //删除地址
    private void httpDetele(int adId) {
        HttpParams params = new HttpParams();
        params.put("userid", SPHelper.getString(MeConstant.UID, ""));
        params.put("ids", adId);
        HpGo.newInstance().HttpGo(mContext, MyUrl.DELETE_ADDRESS, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                norSrl.autoRefresh();
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 0x114:
                norSrl.autoRefresh();
                break;

        }
    }


}
